﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;

namespace Bot.Application.Entities.Service
{
    public class EntityAppService : BaseAppService, IEntityAppService
    {
        private IEntityDomainService _domainService;
        private IChatbotDomainService _chatbotDomainService;

        public EntityAppService(IEntityDomainService domainService,
            IChatbotDomainService chatbotDomainService) : base()
        {
            this._domainService = domainService;
            _chatbotDomainService = chatbotDomainService;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.ENTITY, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.ENTITY, AuditAction.CREATE)]
        public Dto.EntityDto Add(Guid botId, EntityCreateDto dto)
        {
            dto.BotId = botId;
            Entity entity = _domainService.Create(Mapper.Map<Entity>(dto));
            return Mapper.Map<Dto.EntityDto>(entity);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.ENTITY, AuditAction.UPDATE)]
        public Dto.EntityDto Update(Guid id, EntityUpdateDto dto)
        {
            Entity entity = _domainService.Update(Mapper.Map<EntityUpdateBo>(dto));
            return Mapper.Map<Dto.EntityDto>(entity);
        }

        public Dto.EntityDto Get(Guid id)
        {
            Entity entity = _domainService.Get(id, "keywords");
            return Mapper.Map<Dto.EntityDto>(entity);
        }

        public IEnumerable<Dto.EntityDto> GetList(Guid botId, string include="")
        {
            IEnumerable<Entity> list = _domainService.ListCustomEntities(botId, include);

            return Mapper.Map<IEnumerable<Dto.EntityDto>>(list);
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="botId"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public OperationDto Import(Guid botId, string filePath)
        {
            Guid jobId = Guid.NewGuid();
            var bot = _chatbotDomainService.Get(botId);
            Operation operation = _domainService.Import(jobId, bot, filePath);
            return Mapper.Map<OperationDto>(operation);
        }
    }
}
